<?php
class ModelSaleWechat extends Model {
    public function getWechats($data = array()) {
        $sql = "SELECT * FROM " . DB_PREFIX . "wechat_user ";

        $sort_data = array(
            'openid',
            'id',
            'nickname',
            'sex',
            'province',
            'city',
            'country',
            'subscribe',
            'share_status',
            'referee_id',
            'date_added',
            'lottery_times',
        );

        if (isset($data['sort']) && in_array($data['sort'], $sort_data)) {
            $sql .= " ORDER BY " . $data['sort'];
        } else {
            $sql .= " ORDER BY id";
        }

        if (isset($data['order']) && ($data['order'] == 'DESC')) {
            $sql .= " DESC";
        } else {
            $sql .= " ASC";
        }

        if (isset($data['start']) || isset($data['limit'])) {
            if ($data['start'] < 0) {
                $data['start'] = 0;
            }

            if ($data['limit'] < 1) {
                $data['limit'] = 20;
            }

            $sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit'];
        }

        $query = $this->db->query($sql);
        return $query->rows;
    }

	public function deleteWechat($openid) {
		$this->db->query("DELETE FROM " . DB_PREFIX . "wechat_user WHERE openid = '" . $openid . "'");
		$this->db->query("DELETE FROM " . DB_PREFIX . "wechat_user_prize WHERE openid = '" . $openid. "'");
	}

    public function getTotalWechats() {
        $query = $this->db->query("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "wechat_user");

        return $query->row['total'];
    }

    public function getuserinfo($openid) {
        $query=$this->db->query("SELECT * FROM " . DB_PREFIX . "wechat_user WHERE openid='".$openid."'");
        return $query->row;
    }
    public function getuserLotteryinfo($openid) {
        $query=$this->db->query("SELECT * FROM " . DB_PREFIX . "wechat_user_prize WHERE openid='".$openid."'");
        return $query->row;
    }
    public function getWechatuserLottery($openid){
        $query=$this->db->query("SELECT * FROM " . DB_PREFIX . "wechat_user_prize WHERE openid='".$openid."'");

        return $query->rows;
    }
}
?>